package com.seckill.order.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.seckill.goods.feign.SkuFeign;
import com.seckill.goods.pojo.Sku;
import com.seckill.order.dao.OrderMapper;
import com.seckill.order.pojo.Order;
import com.seckill.order.service.OrderService;
import com.seckill.util.IdWorker;
import com.seckill.util.Result;
import com.seckill.util.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.kafka.core.KafkaTemplate;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @program: seckill-parent
 * @description: 订单处理业务逻辑
 * @author: zhanghz001
 * @create: 2021-06-30 16:05
 **/
// @Service
public class ZhzOrderServiceImpl implements OrderService {
    @Autowired
    private RedisTemplate redisTemplate;
    
    @Autowired
    private IdWorker idWorker;
    @Autowired
    private KafkaTemplate kafkaTemplate;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private SkuFeign skuFeign;
    
    @Override
    public void hotAdd(Map<String, String> orderMap) throws IOException {
        
    }
    
    @Override
    public int add(Order order) {
        //查看是否在24小时内是否抢过        
        String alreadyBuyKey = "USER_" + order.getUsername() + "_SKU_ID_" + order.getSkuId();
        Boolean hasKey = redisTemplate.hasKey(alreadyBuyKey);
        
        //买过了就不能再买了
        if (hasKey != null && hasKey) {
            return 0;
        }
        
        //没有抢过 
        //200 库存递减成功 
        //405 库存不足 
        //205 商品是热卖商品
        //0 库存递减失败
        Result<Sku> result = skuFeign.dcount(order.getSkuId(), order.getTotalNum());
        
        if (result.getCode() == StatusCode.DECOUNT_OK) {
            //库存递减成功
            Sku sku = result.getData();
            
            //下单餐桌
            order.setId("No" + idWorker.nextId());
            order.setOrderStatus("0");
            order.setPayStatus("0");
            order.setConsignStatus("0");
            order.setName(sku.getName());
            order.setPrice(sku.getSeckillPrice() * order.getTotalNum());
            orderMapper.insertSelective(order);
            
            //为了防止24小时内重复抢购,需要对抢购过的商品和用户进行保存
            redisTemplate.boundValueOps(alreadyBuyKey).set("", 1, TimeUnit.MINUTES);
            return StatusCode.ORDER_OK;
        } else if (result.getCode() == StatusCode.DECOUNT_NUM) {
            //库存不足
            return StatusCode.DECOUNT_NUM;
        } else if (result.getCode() == StatusCode.DECOUNT_HOT) {
            //商品是热点商品
            
            //防止重复排队处理
            String key = "SKU_" + order.getSkuId();
            Long increment = redisTemplate.boundHashOps(key).increment(alreadyBuyKey, 1L);
            if (increment != null && increment == 1) {
                //需要添加到kakfa队列中
                Map<String, Object> orderInfo = new HashMap<>();
                orderInfo.put("id", order.getSkuId());
                orderInfo.put("username", order.getUsername());
                kafkaTemplate.send("neworder", JSON.toJSONString(orderInfo));
            }
            return StatusCode.ORDER_QUEUE;
        } else {
            //库存递减失败
            return result.getCode();
        }
    }
    
    @Override
    public PageInfo<Order> findPage(Order order, int page, int size) {
        return null;
    }
}
